// main.qml
// Bezel-less-Widget 适配到 Qt for HarmonyOS
// 无边框窗口，支持拖动和缩放

import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15

ApplicationWindow {
    id: root
    width: Screen.width > 0 ? Screen.width : 1200
    height: Screen.height > 0 ? Screen.height : 800
    visible: true
    title: "Bezel-less-Widget - 无边框窗口演示"
    
    // 无边框窗口标志 - 使用 FramelessWindowHint 移除系统边框
    flags: Qt.Window | Qt.FramelessWindowHint
    
    // 设置窗口背景为主题背景色，避免恢复窗口时出现白色或黑色
    color: getThemeColor("mainBgStart")
    
    // 窗口属性
    property int titleBarHeight: 50  // 标题栏高度（增大）
    property int minWidth: 700
    property int minHeight: 500
    
    // 当前样式主题
    property string currentTheme: "lightblue"  // lightblue, flatgray, blacksoft
    
    // 主题颜色定义
    property var themeColors: {
        "lightblue": {
            titleBarStart: "#CBF2F7",
            titleBarEnd: "#FFF8D5",
            mainBgStart: "#D5DEE7",
            mainBgMiddle: "#FFAFBD",
            mainBgEnd: "#C9FFBF",
            textColor: "#386487",
            buttonBg: "#EAF7FF"
        },
        "flatgray": {
            titleBarStart: "#667EEA",
            titleBarEnd: "#764BA2",
            mainBgStart: "#667EEA",
            mainBgMiddle: "#764BA2",
            mainBgEnd: "#F093FB",
            textColor: "#FFFFFF",
            buttonBg: "#8B7EC8"
        },
        "blacksoft": {
            titleBarStart: "#2B2B2B",
            titleBarEnd: "#3B3B3B",
            mainBgStart: "#1E1E1E",
            mainBgMiddle: "#2A2A2A",
            mainBgEnd: "#353535",
            textColor: "#E0E0E0",
            buttonBg: "#404040"
        }
    }
    
    // 获取当前主题颜色
    function getThemeColor(key) {
        return themeColors[currentTheme][key] || themeColors["lightblue"][key]
    }
    
    // 加载样式
    function loadTheme(themeName) {
        currentTheme = themeName
        console.log("Bezel_less_Widget: 切换到主题:", themeName)
    }
    
    // 主内容区域
    Rectangle {
        id: contentRect
        anchors.fill: parent
        color: getThemeColor("mainBgStart")
        
        // 标题栏
        Rectangle {
            id: titleBar
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            height: titleBarHeight
            z: 100
            
            // 渐变背景 - 根据主题动态变化
            gradient: Gradient {
                GradientStop { position: 0.04; color: getThemeColor("titleBarStart") }
                GradientStop { position: 0.96; color: getThemeColor("titleBarEnd") }
            }
            
            // 标题栏内容
            RowLayout {
                anchors.fill: parent
                anchors.margins: 2
                spacing: 8
                
                // 图标
                Image {
                    Layout.preferredWidth: 30
                    Layout.preferredHeight: 30
                    source: "qrc:/res/title.svg"
                    fillMode: Image.PreserveAspectFit
                }
                
                // 标题文本
                Text {
                    Layout.fillWidth: true
                    text: "Ryan的百宝箱"
                    font.pixelSize: 28
                    font.family: "Hack Nerd Font"
                    color: getThemeColor("textColor")
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                
                // 最小化按钮
                Button {
                    id: minBtn
                    Layout.preferredWidth: 44
                    Layout.preferredHeight: 44
                    background: Rectangle {
                        anchors.fill: parent
                        color: minBtn.hovered ? "#E0E0E0" : "transparent"
                        radius: 4
                    }
                    Image {
                        anchors.centerIn: parent
                        width: 28
                        height: 28
                        source: "qrc:/res/zoom.svg"
                        fillMode: Image.PreserveAspectFit
                    }
                    onClicked: {
                        root.showMinimized()
                    }
                }
                
                // 最大化/还原按钮
                Button {
                    id: maxBtn
                    Layout.preferredWidth: 44
                    Layout.preferredHeight: 44
                    background: Rectangle {
                        anchors.fill: parent
                        color: maxBtn.hovered ? "#E0E0E0" : "transparent"
                        radius: 4
                    }
                    Image {
                        id: maxIcon
                        anchors.centerIn: parent
                        width: 28
                        height: 28
                        source: root.visibility === Window.Maximized ? "qrc:/res/maxmin.svg" : "qrc:/res/max.svg"
                        fillMode: Image.PreserveAspectFit
                    }
                    onClicked: {
                        if (root.visibility === Window.Maximized) {
                            root.showNormal()
                        } else {
                            root.showMaximized()
                        }
                    }
                }
                
                // 关闭按钮
                Button {
                    id: closeBtn
                    Layout.preferredWidth: 44
                    Layout.preferredHeight: 44
                    background: Rectangle {
                        anchors.fill: parent
                        color: closeBtn.hovered ? "#FF4444" : "transparent"
                        radius: 4
                    }
                    Image {
                        anchors.centerIn: parent
                        width: 28
                        height: 28
                        source: "qrc:/res/close.svg"
                        fillMode: Image.PreserveAspectFit
                    }
                    onClicked: {
                        root.close()
                    }
                }
            }
            
            // 标题栏区域（在 HarmonyOS 上窗口移动由系统管理）
            MouseArea {
                anchors.fill: parent
                anchors.rightMargin: 140  // 为按钮留出空间（增大）
                acceptedButtons: Qt.LeftButton
                // 注意：在 HarmonyOS 上，窗口移动由系统管理，这里只做占位
            }
        }
        
        // 主窗口内容
        Rectangle {
            id: mainContent
            anchors.top: titleBar.bottom
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            
            // 渐变背景 - 根据主题动态变化
            gradient: Gradient {
                GradientStop { position: 0.0; color: getThemeColor("mainBgStart") }
                GradientStop { position: 0.5; color: getThemeColor("mainBgMiddle") }
                GradientStop { position: 1.0; color: getThemeColor("mainBgEnd") }
            }
            
            // 内容布局
            ColumnLayout {
                anchors.fill: parent
                anchors.margins: 20
                spacing: 20
                
                Text {
                    Layout.fillWidth: true
                    text: "无边框窗口演示"
                    font.pixelSize: 48
                    font.bold: true
                    color: getThemeColor("textColor")
                    horizontalAlignment: Text.AlignHCenter
                }
                
                // 样式切换按钮
                RowLayout {
                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignHCenter
                    spacing: 20
                    
                    Rectangle {
                        id: style1Container
                        Layout.preferredWidth: 140
                        Layout.preferredHeight: 140
                        radius: 50
                        gradient: Gradient {
                            GradientStop { position: 0.0; color: "#CBF2F7" }
                            GradientStop { position: 1.0; color: "#ACE0F9" }
                        }
                        border.width: 4
                        border.color: currentTheme === "lightblue" ? "#386487" : "#CCCCCC"
                        
                        Text {
                            anchors.centerIn: parent
                            text: "样式1\n浅蓝"
                            font.pixelSize: 28
                            color: "#386487"
                            horizontalAlignment: Text.AlignHCenter
                        }
                        
                        MouseArea {
                            anchors.fill: parent
                            onClicked: {
                                console.log("Bezel_less_Widget: 点击样式1按钮")
                                loadTheme("lightblue")
                            }
                        }
                    }
                }
                
                Text {
                    Layout.fillWidth: true
                    text: "当前主题: " + currentTheme
                    font.pixelSize: 36
                    color: getThemeColor("textColor")
                    horizontalAlignment: Text.AlignHCenter
                }
                
                Text {
                    Layout.fillWidth: true
                    text: "提示：\n- 点击标题栏按钮可以最小化/最大化/关闭窗口\n- 双击标题栏可以最大化/还原\n- 在 HarmonyOS 上，窗口移动和缩放由系统管理"
                    font.pixelSize: 28
                    color: getThemeColor("textColor")
                    horizontalAlignment: Text.AlignHCenter
                    wrapMode: Text.WordWrap
                }
            }
        }
        
        // 边框区域（在 HarmonyOS 上窗口缩放由系统管理）
        // 注意：HarmonyOS 上的窗口缩放由系统管理，这里只做占位
    }
    
    // 双击标题栏最大化/还原
    MouseArea {
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.right: parent.right
        height: titleBarHeight
        anchors.rightMargin: 140
        acceptedButtons: Qt.LeftButton
        onDoubleClicked: {
            if (root.visibility === Window.Maximized) {
                root.showNormal()
            } else {
                root.showMaximized()
            }
        }
    }
}
